package com.it.orderservice.controller;




import com.it.orderservice.entity.Order;
import com.it.orderservice.service.OrderService;
import com.it.productservice.entity.Product;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping("/order/{id}")
    public ResponseEntity<Order> findOrderById(@PathVariable Integer id){
//查询订单
        Order order = orderService.getById(id);
//远程调用商品服务，通过商品id查询商品
        ResponseEntity<Product> entity =
                restTemplate.getForEntity("http://127.0.0.1:8001/product/" +
                                order.getProductId(),
                        Product.class);
//设置订单的商品对象
        order.setProduct(entity.getBody());
        return ResponseEntity.ok(order);
    }
}
